﻿var app = angular.module("app", ['ngRoute']);
app.config(function ($routeProvider) {
    $routeProvider.when("/", {
        templateUrl: "../Content/ngViews/ngHome.html",
        controller: "ngHomeController"
    }).when("/colleges", {
        templateUrl: "../Content/ngViews/ngCollegeIndex.html",
        controller: "ngCollegeIndexController"
    }).when("/addcollege", {
        templateUrl: "../Content/ngViews/ngAddCollege.html",
        controller: "ngCollegeAddController"
    }).when("/collegedetail/:id", {
        templateUrl: "../Content/ngViews/ngCollegeDetail.html",
        controller: "collegeDetailController"
    }).otherwise({
        templateUrl:"Views/404.cshtml"
    })
});
app.factory('Colleges', function () { return []; });
app.controller("ngHomeController", function ($scope) { });
app.controller('ngCollegeIndexController', function ($scope, Colleges, $http) {
    $scope.Colleges = Colleges;
    $http.get("/api/values").success(function (data) {
        while (Colleges.length) {
            Colleges.pop();
        }
        for (x in data.Colleges) {
            Colleges.push(data.Colleges[x]);
        }
        })
        .error(function(){
            console.log("Error on get")
        });
});
app.controller('ngCollegeDetailController', function (Colleges, $scope, $routeParams, $http) {
    $scope.currentIndex = $routeParams.id;
    $scope.college = Colleges[$scope.currentIndex];
    $http.get("/api/values/:id").success(function (data) {
        while (Colleges.length) {
            Colleges.pop();
        }
        for (x in data) {
            Colleges.push(data[x]);
        }
        $scope.college = Colleges[$scope.currentIndex]
    })
        .error(function () {
            console.log("Error on get")
        });
});
app.controller('ngCollegeAddController', function ($scope, $http, $location) {
    $scope.addCollege = function (college) {
        $http.post("api/values/", college).success(function (data) {
            college.Id = data;
            $scope.colleges.push({ university: college.university, location: college.location, summary: college.summary, image: college.image, id: college.Id });
            $scope.university = "";
            $scope.location = "";
            $scope.summary = "";
            $scope.image = "";
            $location.path('/collegedetail/' + College.indexOf(college));
        }).error(function (data) {
            console.log(JSON.stringify(data));
        });
    };
});